from .dataset_behavior import BasicBehavior
import numpy as np

"""
灰度图像灰度变换的相应行为代码
"""

__all__ = ["GrayAutoGamma", "GrayLogAutoNoram"]

class GrayAutoGamma(BasicBehavior):
    """
    自动gamma灰度变换
    """
    def __init__(self, dest_gary = 0.5):
        self.dest_gary = 0.5

    def __call__(self, img: np.ndarray):
        img_mean = np.mean(img)
        gamma = np.log10(self.dest_gary) / np.log10(img_mean)
        img = np.power(img, gamma)
        return img

    def __str__(self):
        return f"GrayAutoGamma(dest_gary={self.dest_gary})"


class GrayLogAutoNoram(BasicBehavior):
    """
    自动Log+sigmod灰度变换
    """
    def __init__(self, sigmod_gain):
        self.sigmod_gain = sigmod_gain

    def __call__(self, x: np.ndarray):
        img = np.log(1+x)                                           # log
        img = (img - np.min(img)) / (np.max(img) - np.min(img))     # 归一
        mean_val = np.mean(img)                                     # 计算均值
        img = self.sigmod((img-mean_val) * self.sigmod_gain)
        return img

    def sigmod(self, x: np.ndarray):
        return 1/(1+np.power(np.e, -x))


